import 'package:flutter_axrjc/datasource/network/api/KnowledgeApi.dart';
import 'package:flutter_axrjc/datasource/network/bean/ArticleTabBean.dart';
import 'package:flutter_axrjc/datasource/network/request/exp.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'state.dart';

class KnowledgeForEyeLogic extends GetxController
    with GetTickerProviderStateMixin {
  final KnowledgeForEyeState state = KnowledgeForEyeState();

  var page = 1;

  var refreshController = RefreshController(initialRefresh: true);

  late ArticleTabBean bean;
  @override
  void onInit() {
    super.onInit();
    bean = Get.arguments as ArticleTabBean;
  }

  Future<void> getPage({required bool isRefresh}) async {
    if (isRefresh) {
      page = 1;
      state.list.clear();
    } else {
      page++;
    }
    try {
      state.list.addAll(await KnowledgeApi.getTopThree(
          limit: "10",
          page: '$page',
          categoryId: "${bean.categoryId}",
          ifText: false));
    } on ApiException catch (e) {
      EasyLoading.showToast(e.message);
    } catch (e) {
      EasyLoading.showToast("$e");
    }
    if (isRefresh) {
      refreshController.refreshCompleted();
    } else {
      refreshController.loadComplete();
    }
  }
}
